package it.mp.multitask;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class DialogoKill extends Activity {
	
	private String packageName=null;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.dialogo_kill);
		Intent intent=getIntent();		 
		packageName=intent.getExtras().getString(Main.PACKAGE_NAME);
		ImageView iv=(ImageView)findViewById(R.id.IV_application);		
		try {
			iv.setImageDrawable(getPackageManager().getApplicationIcon(packageName));
		} catch (NameNotFoundException e) {
		}
		
		try {
			findViewById(R.id.TV_applicationName).setVisibility(View.VISIBLE);
			((TextView)findViewById(R.id.TV_applicationName)).setText(getPackageManager().getApplicationLabel(getPackageManager().getApplicationInfo(packageName, 0)));
		} catch (NameNotFoundException e) {
			findViewById(R.id.TV_applicationName).setVisibility(View.INVISIBLE);
		}
		
		findViewById(R.id.BTN_cancel).setOnClickListener(new OnClickListener(){
			public void onClick(View arg0) {
				finish();				
			}			
		});
		findViewById(R.id.BTN_kill).setOnClickListener(new OnClickListener(){
			public void onClick(View arg0) {
				killApplication(packageName);
				finish();				
			}			
		});
				
	}
	private void killApplication(String packageName){				
		ActivityManager actvityManager = (ActivityManager) DialogoKill.this.getSystemService(ACTIVITY_SERVICE);
		actvityManager.restartPackage(packageName);
		SharedPreferences prefs = getSharedPreferences(Main.SP_OPTIONS, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putBoolean(it.mp.multitask.Main.KILL_SENZA_CONFERMA, ((CheckBox)findViewById(R.id.CB_question)).isChecked());
		editor.commit();		
	}
}
